<?php

include_once '../inc/db_util.php';

function view($filter) {
    $array_announce = array();
    if (!is_null($filter)) {
        $jsArray = json_decode($filter, false);
        $query = mysql_query("SELECT * from tb_pengumuman_matkul pm
                        JOIN tb_det_matkul dm ON dm.id_det_matkul = pm.id_det_matkul
                        JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
                        JOIN tb_dosen td ON dm.nip = td.nip WHERE 
                        dm.id_det_matkul='" . $jsArray->{'Id'} . "'
                    ");
    } else {
        $query = mysql_query("SELECT * from tb_pengumuman_matkul pm
                        JOIN tb_det_matkul dm ON dm.id_det_matkul = pm.id_det_matkul
                        JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
                        JOIN tb_dosen td ON dm.nip = td.nip ORDER BY tanggal_create DESC
                    ");
    }

    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_pengumuman_matkul'];
        $row_array['Perihal'] = $row['prihal'];
        $row_array['Info'] = $row['isi'];
        $row_array['ImageUrl'] = $row['image_url'];
        $row_array['Created'] = $row['tanggal_create'];
        $row_array['DetMatkul']['Id'] = $row['id_det_matkul'];
        $row_array['DetMatkul']['TahunAjaran'] = $row['tahun_ajaran'];
        $row_array['DetMatkul']['Creator']['Nip'] = $row['nip'];
        $row_array['DetMatkul']['Creator']['Nama'] = $row['namadosen'];
        $row_array['DetMatkul']['Matkul']['Id'] = $row['id_matkul'];
        $row_array['DetMatkul']['Matkul']['Kode'] = $row['kode_matkul'];
        $row_array['DetMatkul']['Matkul']['Nama'] = $row['matkul'];
        $row_array['DetMatkul']['Matkul']['SKS'] = $row['sks'];
        $row_array['DetMatkul']['Matkul']['Semester'] = $row['semester'];

        array_push($array_announce, $row_array);
    }
    return json_encode($array_announce);
}

function detail($filter) {
    $array_announce = array();
    $jsArray = json_decode($filter, false);
    $query = mysql_query("SELECT * from tb_pengumuman_matkul pm
                    JOIN tb_det_matkul dm ON dm.id_det_matkul = pm.id_det_matkul
					JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
                    JOIN tb_dosen td ON dm.nip = td.nip WHERE 
                    id_pengumuman_matkul='" . $jsArray->{'Id'} . "'
                    ");
    
    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_pengumuman_matkul'];
        $row_array['Perihal'] = $row['prihal'];
        $row_array['Info'] = $row['isi'];
        $row_array['ImageUrl'] = $row['image_url'];
        $row_array['Created'] = $row['tanggal_create'];
        $row_array['DetMatkul']['Id'] = $row['id_det_matkul'];
        $row_array['DetMatkul']['TahunAjaran'] = $row['tahun_ajaran'];
        $row_array['DetMatkul']['Creator']['Nip'] = $row['nip'];
        $row_array['DetMatkul']['Creator']['Nama'] = $row['namadosen'];
        $row_array['DetMatkul']['Matkul']['Id'] = $row['id_matkul'];
        $row_array['DetMatkul']['Matkul']['Kode'] = $row['kode_matkul'];
        $row_array['DetMatkul']['Matkul']['Nama'] = $row['matkul'];
        $row_array['DetMatkul']['Matkul']['SKS'] = $row['sks'];
        $row_array['DetMatkul']['Matkul']['Semester'] = $row['semester'];

        array_push($array_announce, $row_array);
    }
    return json_encode($array_announce);
}

function find($keyword) {
    $array_announce = array();
    $jsArray = json_decode($keyword, false);
    $query = mysql_query("SELECT * FROM tb_pengumuman_matkul pm
                JOIN tb_det_matkul dm ON dm.id_det_matkul = pm.id_det_matkul
                JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
                JOIN tb_dosen td ON dm.nip = td.nip 
				WHERE prihal LIKE '%" . $jsArray->{'Info'} . "%' OR isi LIKE '%" . $jsArray->{'Info'} . "%'
				ORDER BY tanggal_create DESC");

    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_pengumuman_matkul'];
        $row_array['Perihal'] = $row['prihal'];
        $row_array['Info'] = $row['isi'];
        $row_array['ImageUrl'] = $row['image_url'];
        $row_array['Created'] = $row['tanggal_create'];
        $row_array['DetMatkul']['Id'] = $row['id_det_matkul'];
        $row_array['DetMatkul']['TahunAjaran'] = $row['tahun_ajaran'];
        $row_array['DetMatkul']['Creator']['Nip'] = $row['nip'];
        $row_array['DetMatkul']['Creator']['Nama'] = $row['namadosen'];
        $row_array['DetMatkul']['Matkul']['Id'] = $row['id_matkul'];
        $row_array['DetMatkul']['Matkul']['Kode'] = $row['kode_matkul'];
        $row_array['DetMatkul']['Matkul']['Nama'] = $row['matkul'];
        $row_array['DetMatkul']['Matkul']['SKS'] = $row['sks'];
        $row_array['DetMatkul']['Matkul']['Semester'] = $row['semester'];

        array_push($array_announce, $row_array);
    }
    return json_encode($array_announce);
}

function create($json) {
    $jsArray = json_decode($json, false);

    $sql = "INSERT INTO tb_pengumuman_matkul (id_det_matkul, prihal, isi, image_url, tanggal_create)
			VALUES (
			'" . $jsArray->{'DetMatkul'}->{'Id'} . "',
			'" . $jsArray->{'Perihal'} . "',
			'" . $jsArray->{'Info'} . "',
			'" . $jsArray->{'ImageUrl'} . "',
			NOW())";

    return mysql_query($sql);
}

function update($json) {
    $jsArray = json_decode($json, false);
    $sql = "UPDATE tb_pengumuman_matkul SET 
            prihal='" . $jsArray->{'Perihal'} . "',
            isi='" . $jsArray->{'Info'} . "',
			image_url='" . $jsArray->{'ImageUrl'} . "'
            WHERE 
            id_pengumuman_matkul='" . $jsArray->{'Id'} . "'";

    return mysql_query($sql);
}

function delete($json) {
    $jsArray = json_decode($json, false);
    $sql = "DELETE FROM tb_pengumuman_matkul WHERE id_pengumuman_matkul='" . $jsArray->{'Id'} . "'";
    return mysql_query($sql);
}

?>
